<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <%@include file="/WEB-INF/includes/css-file.jsp" %>
    <link rel="stylesheet" href="${ctp}/static/css/main.css">
    <link rel="stylesheet" href="${ctp}/static/css/doc.min.css">
    <style>
        .tree li {
            list-style-type: none;
            cursor: pointer;
        }
    </style>
</head>

<body>
<%
    // 设置导航条上的显示
    pageContext.setAttribute("navInfo", "用户维护");
    // 设置点击高亮效果
    pageContext.setAttribute("curUrl", "permission/user/list");
%>
<%@include file="/WEB-INF/includes/navbar.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="/WEB-INF/includes/sidebar_menu.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <ol class="breadcrumb">
                <li><a href="#">首页</a></li>
                <li><a href="#">数据列表</a></li>
                <li class="active">分配角色</li>
            </ol>
            <div class="panel panel-default">
                <div class="panel-body">
                    <form role="form" class="form-inline">
                        <div class="form-group">
                            <label for="exampleInputPassword1">未分配角色列表</label><br>
                            <select class="form-control unRolesSelect" multiple size="10"
                                    style="width:100px;overflow-y:auto;">
                                <c:forEach items="${unRoles}" var="role">
                                    <option value="${role.id}">${role.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                        <div class="form-group">
                            <ul>
                                <li class="btn btn-default glyphicon glyphicon-chevron-right"></li>
                                <br>
                                <li class="btn btn-default glyphicon glyphicon-chevron-left"
                                    style="margin-top:20px;"></li>
                            </ul>
                        </div>
                        <div class="form-group" style="margin-left:40px;">
                            <label for="exampleInputPassword1">已分配角色列表</label><br>
                            <select class="form-control userRolesSelect" multiple size="10"
                                    style="width:100px;overflow-y:auto;">
                                <c:forEach items="${userRoles}" var="role">
                                    <option value="${role.id}">${role.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="/WEB-INF/includes/js-file.jsp" %>
<%@include file="/WEB-INF/includes/common-js.jsp" %>
<script type="text/javascript">
    $(function () {
        $(".list-group-item").click(function () {
            if ($(this).find("ul")) {
                $(this).toggleClass("tree-closed");
                if ($(this).hasClass("tree-closed")) {
                    $("ul", this).hide("fast");
                } else {
                    $("ul", this).show("fast");
                }
            }
        });
    });

    var userId = ${param.userId};
    $(".glyphicon-chevron-right").click(function () {
        var roleIds = "";
        $(".unRolesSelect option:selected").each(function () {
            roleIds += $(this).val() + ",";
        })
        $.post("${ctp}/permission/user/assignRoles?opt=add&userId="
            + userId + "&roleIds=" + roleIds.substring(0, roleIds.length - 1), function () {
            // 分配成功，刷新页面
            $(".unRolesSelect option:selected").appendTo(".userRolesSelect");
        })
    })
    $(".glyphicon-chevron-left").click(function () {
        var roleIds = "";
        $(".userRolesSelect option:selected").each(function () {
            roleIds += $(this).val() + ",";
        })
        $.post("${ctp}/permission/user/assignRoles?opt=remove&userId="
            + userId + "&roleIds=" + roleIds.substring(0, roleIds.length - 1), function () {
            $(".userRolesSelect option:selected").appendTo(".unRolesSelect");
        })
    })
</script>
</body>
</html>